package model;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;

import misc.XMLManager;



public class DataSource {    
    private Connection connection;
	private String driver;
	private String protocol;
	private String host;
	private String database;
	private String username;
	private String password;
	
	private String status;
	
	public DataSource(String driver, String protocol, String host, String database, String username, String password){
		closeConnection();
		setDriver(driver);
		setProtocol(protocol);
		setHost(host);
		setDatabase(database);
		setUsername(username);
		setPassword(password);
	}
	
	public DataSource(File configFile){
		closeConnection();
		readXml(configFile);
	}
		
	public Connection getConnection(){
		return connection;
	}
	
	private void setConnection(Connection connection){
		this.connection = connection;
	}
	
	private String getDriver(){
		return driver;
	}
	
	private void setDriver(String driver){
		this.driver = driver;
	}
	
	private String getProtocol() {
		return protocol; //"jdbc:mysql" Protocolo inicial da url do servidor.
	}

	private void setProtocol(String protocol) {
		this.protocol = protocol;
	}
	
	private String getHost() {
		return host; //"localhost" ou ip. Caminho do servidor do Banco de Dados.
	}

	private void setHost(String host) {
		this.host = host;
	}

	private String getDatabase() {
		return database; //"mysql" Nome do banco.
	}

	private void setDatabase(String database) {
		this.database = database;
	}

	private String getUsername() {
		return username; //"root" Nome de usuário de acesso ao banco.
	}

	private void setUsername(String username) {
		this.username = username;
	}

	private String getPassword() {
		return password; //"root" Senha de acesso ao banco.
	}

	private void setPassword(String password) {		
		this.password = password;
	}

	public String getStatus(){
		if(status == null){
			status = "Desconectado";
		}
		return status;
	}
	
	private void setStatus(String status){
		this.status = status;
	}
	
	private String getUrl() {
		return getProtocol() + getHost() + "/" + getDatabase(); //"jdbc:mysql://host/database";
	}
	
	public void startConnection(){
		try{
			Class.forName(getDriver());
			setConnection(DriverManager.getConnection(getUrl(), getUsername(), getPassword()));
			setStatus("Conectado com sucesso");
			
		}catch(ClassNotFoundException e){
			setStatus("Não foi possivel realizar conexão");
			System.err.println("O driver expecificado nao foi encontrado.");
			//e.printStackTrace();
		}catch(SQLException e){
			setStatus("Não foi possivel realizar conexão");
			System.err.println("Não foi possível conectar ao Banco de Dados.");
			//e.printStackTrace();
		}
	}
	
	public boolean closeConnection(){
		try{
			if(connection != null){
				getConnection().close();
				setConnection(null);
				setStatus("Desconectado");
			}
			
			return true;
		}catch(SQLException e){
			return false;
		}
	}
		
	public void changeConnection(String driver, String protocol, String host, String database, String username, String password){
		closeConnection();
		setDriver(driver);
		setProtocol(protocol);
		setHost(host);
		setDatabase(database);
		setUsername(username);
		setPassword(password);
	}
	
	public void changeConnection(File configFile){
		closeConnection();
		readXml(configFile);
	}
	
	private void readXml(File configFile){		
		HashMap<String, String> hash = XMLManager.readXMLDocument(XMLManager.loadXMLFile(configFile));		
		setDriver(hash.get("driver"));
		setProtocol(hash.get("protocol"));
		setHost(hash.get("host"));
		setDatabase(hash.get("database"));
		setUsername(hash.get("username"));
		setPassword(hash.get("password"));
	}
	
	public static void main(String[] args) {
		DataSource dataSource = new DataSource(new File("bd/bd_config.xml"));
		System.out.println(dataSource.getStatus());
		dataSource.startConnection();
		System.out.println(dataSource.getStatus());
		dataSource.getDatabase();
		dataSource.closeConnection();
		System.out.println(dataSource.getStatus());
	}
}